package demo;

import java.util.HashMap;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-07-28
 * Time: 11:13
 */

class Counter {
    // 用来保存计数的变量
    public int count;

    synchronized public void increase() {
        count++;
    }

    synchronized public void decrease() {
        count--;
    }

}
public class test1 {

    public static void main(String[] args) {
        Counter counter = new Counter();
        Stack<Integer> stack = new Stack<>();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                counter.increase();
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                counter.decrease();
            }
        });
        t1.start();
        t2.start();

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("count: " + counter.count);
    }
}
